<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>string</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../dom.html" title="Document Model">
<link rel="prev" href="value.html" title="value">
<link rel="next" href="array.html" title="array">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="value.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="array.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.dom.string"></a><a class="link" href="string.html" title="string">string</a>
</h3></div></div></div>
<p>
        Modifiable sequences of characters are represented using objects of type
        <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>.
        The interface and functionality of <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a> is the same as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code>
        except that:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>
            is not a class template,
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>
            uses <code class="computeroutput"><span class="keyword">char</span></code> as its character
            type,
          </li>
<li class="listitem">
            redundant overloads for string operations have been replaced with a
            <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a>
            based interface,
          </li>
<li class="listitem">
            access to characters in the range <code class="computeroutput"><span class="special">[</span><span class="identifier">size</span><span class="special">(),</span> <span class="identifier">capacity</span><span class="special">())</span></code>
            is permitted, and
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a>
            is used instead of <a href="https://en.cppreference.com/w/cpp/named_req/Allocator" target="_top"><span class="emphasis"><em>Allocator</em></span></a>.
          </li>
</ul></div>
<p>
        With augmented interface, operations requiring an input string are implemented
        as a single overload with a parameter of type <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a>, and can accept most
        string-like objects. Objects such as null terminated character pointers,
        <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>, <code class="computeroutput"><span class="identifier">json</span><span class="special">::</span><span class="identifier">string</span></code>,
        subranges of strings, and objects convertible to <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a> can all be passed to
        these functions.
      </p>
<pre class="programlisting"><span class="identifier">json</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">str</span> <span class="special">=</span> <span class="string">"Boost.JSON"</span><span class="special">;</span>
<span class="identifier">json</span><span class="special">::</span><span class="identifier">string_view</span> <span class="identifier">sv</span> <span class="special">=</span> <span class="identifier">str</span><span class="special">;</span>

<span class="comment">// all of these call compare(string_view)</span>
<span class="identifier">str</span><span class="special">.</span><span class="identifier">compare</span><span class="special">(</span><span class="identifier">sv</span><span class="special">);</span>

<span class="identifier">str</span><span class="special">.</span><span class="identifier">compare</span><span class="special">(</span><span class="identifier">sv</span><span class="special">.</span><span class="identifier">substr</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">5</span><span class="special">));</span>

<span class="identifier">str</span><span class="special">.</span><span class="identifier">compare</span><span class="special">(</span><span class="identifier">str</span><span class="special">);</span>

<span class="identifier">str</span><span class="special">.</span><span class="identifier">compare</span><span class="special">(</span><span class="string">"Boost"</span><span class="special">);</span>
</pre>
<p>
        More formally, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> member function overloads that accept
        any of the following parameter combinations as an input string:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> parameter, or
          </li>
<li class="listitem">
            a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> parameter and two <code class="computeroutput"><span class="identifier">size_type</span></code> parameters that specify a
            substring, or
          </li>
<li class="listitem">
            a parameter of a type convertible to <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a>, or
          </li>
<li class="listitem">
            a parameter of a type convertible to <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a> and two <code class="computeroutput"><span class="identifier">size_type</span></code> parameters that specify a
            substring, or
          </li>
<li class="listitem">
            a <code class="computeroutput"><span class="identifier">const_pointer</span></code>, or
          </li>
<li class="listitem">
            a parameter of type <code class="computeroutput"><span class="identifier">const_pointer</span></code>
            and a <code class="computeroutput"><span class="identifier">size_type</span></code> parameter
            that specifies the length of the string
          </li>
</ul></div>
<p>
        are replaced with an overload accepting a <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a> parameter.
      </p>
<p>
        This design removes several redundant overloads from the interface. For example,
        the 11 overloads of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">insert</span></code> are reduced to just 3 in <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>, while still providing identical
        functionality. In addition to these changes, overloads taking a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">initializer_list</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span></code>
        parameter have been removed. Such overloads have little use, as they serve
        as little more than a wrappers for arrays with an inefficient syntax:
      </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">sstr</span> <span class="special">=</span> <span class="string">"hello"</span><span class="special">;</span>

<span class="identifier">json</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">jstr</span> <span class="special">=</span> <span class="string">"hello"</span><span class="special">;</span>

<span class="identifier">assert</span><span class="special">(</span><span class="identifier">sstr</span><span class="special">.</span><span class="identifier">append</span><span class="special">({</span><span class="char">'w'</span><span class="special">,</span> <span class="char">'o'</span><span class="special">,</span> <span class="char">'r'</span><span class="special">,</span> <span class="char">'l'</span><span class="special">,</span> <span class="char">'d'</span><span class="special">})</span> <span class="special">==</span> <span class="string">"helloworld"</span><span class="special">);</span>

<span class="comment">// such syntax is inefficient, and the same can</span>
<span class="comment">// be achieved with a character array.</span>

<span class="identifier">assert</span><span class="special">(</span><span class="identifier">jstr</span><span class="special">.</span><span class="identifier">append</span><span class="special">(</span><span class="string">"world"</span><span class="special">)</span> <span class="special">==</span> <span class="string">"helloworld"</span><span class="special">);</span>
</pre>
<p>
        With the removal of overloads that specify parameters for a substring, a
        member function <code class="computeroutput"><span class="identifier">subview</span></code> that
        returns a <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a> is provided to facilitate
        cheap substring operations:
      </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">sstr1</span> <span class="special">=</span> <span class="string">"helloworld"</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">sstr2</span> <span class="special">=</span> <span class="string">"world"</span><span class="special">;</span>

<span class="identifier">json</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">jstr1</span> <span class="special">=</span> <span class="string">"helloworld"</span><span class="special">;</span>
<span class="identifier">json</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">jstr2</span> <span class="special">=</span> <span class="string">"world"</span><span class="special">;</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jstr2</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">jstr1</span><span class="special">.</span><span class="identifier">subview</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">5</span><span class="special">))</span> <span class="special">==</span> <span class="string">"helloworld"</span> <span class="special">);</span>

<span class="comment">// this is equivalent to</span>
<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">sstr2</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">sstr1</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">5</span><span class="special">)</span> <span class="special">==</span> <span class="string">"helloworld"</span> <span class="special">);</span>
</pre>
<p>
        A <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>
        may be constructed using the default memory resource without incurring any
        memory allocations. Alternatively, a <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a> can be provided explicitly:
      </p>
<pre class="programlisting"><span class="identifier">string</span> <span class="identifier">str1</span><span class="special">;</span> <span class="comment">// empty string, uses the default memory resource</span>

<span class="identifier">string</span> <span class="identifier">str2</span><span class="special">(</span> <span class="identifier">make_shared_resource</span><span class="special">&lt;</span><span class="identifier">monotonic_resource</span><span class="special">&gt;()</span> <span class="special">);</span> <span class="comment">// empty string, uses a counted monotonic resource</span>
</pre>
<h5>
<a name="json.dom.string.h0"></a>
        <span class="phrase"><a name="json.dom.string.formatted_output"></a></span><a class="link" href="string.html#json.dom.string.formatted_output">Formatted
        Output</a>
      </h5>
<p>
        When a <a class="link" href="../ref/boost__json__string.html" title="string"><code class="computeroutput"><span class="identifier">string</span></code></a>
        is formatted to a <a href="https://en.cppreference.com/w/cpp/io/basic_ostream" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code></a>,
        the result is a valid JSON. That is, the result will be double quoted and
        the contents properly escaped per the JSON specification.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="value.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="array.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
